﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WPFGame.Logic;

namespace WPFGame.Controls
{
    /// <summary>
    /// Hero.xaml 的交互逻辑
    /// </summary>
    public partial class Hero : UserControl
    {
        #region DependencyProperty

        public static readonly DependencyProperty XProperty =
            DependencyProperty.Register("X", typeof(double), typeof(Hero), new PropertyMetadata((double)0));

        public static readonly DependencyProperty YProperty =
           DependencyProperty.Register("Y", typeof(double), typeof(Hero), new PropertyMetadata((double)0));

        public static readonly DependencyProperty DirectionProperty =
           DependencyProperty.Register("Direction", typeof(double), typeof(Hero), new PropertyMetadata((double)0));

        /// <summary>
        /// X坐标(关联属性)
        /// </summary>
        public double X
        {
            get { return (double)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }

        /// <summary>
        /// Y坐标(关联属性)
        /// </summary>
        public double Y
        {
            get { return (double)GetValue(YProperty); }
            set { SetValue(YProperty, value); }
        }

        /// <summary>
        /// 当前朝向:0朝上4朝下,顺时针依次为0,1,2,3,4,5,6,7(关联属性)
        /// </summary>
        public double Direction
        {
            get { return (double)GetValue(DirectionProperty); }
            set { SetValue(DirectionProperty, value); }
        }

        #endregion

     
        private string _imageAddress;
        private DispatcherTimer _heroTimer = new DispatcherTimer();


        /// <summary>
        /// 英雄线程
        /// </summary>
        public DispatcherTimer HeroTimer
        {
            get { return _heroTimer; }
            set { _heroTimer = value; }
        }

        /// <summary>
        /// 英雄图片源目录地址
        /// </summary>
        public string ImageAddress
        {
            get { return _imageAddress; }
            set { _imageAddress = value; }
        }

        BitmapSource[,] _HeroSource;
        /// <summary>
        /// 英雄当前调用的图片源(二维数组):第一个表示角色方向,0朝上4朝下,顺时针依次为0,1,2,3,4,5,6,7;第二个表示该方向帧数
        /// </summary>
        public BitmapSource[,] HeroSource
        {
            get { return _HeroSource; }
            set { _HeroSource = value; }
        }

        int _DirectionNum = 8;
        /// <summary>
        /// 英雄方向数量,默认为8个方向
        /// </summary>
        public int DirectionNum
        {
            get { return _DirectionNum; }
            set { _DirectionNum = value; }
        }

        Actions _Action = Actions.Stop;
        /// <summary>
        /// 英雄当前动作状态
        /// </summary>
        public Actions Action
        {
            get { return _Action; }
            set { _Action = value; }
        }

        Actions _OldAction = Actions.Stop;
        /// <summary>
        /// 英雄之前动作状态
        /// </summary>
        public Actions OldAction
        {
            get { return _OldAction; }
            set { _OldAction = value; }
        }

        int[] _EachActionFrameRange;
        /// <summary>
        /// 英雄各动作对应的帧列范围(暂时只有5个动作)
        /// </summary>
        public int[] EachActionFrameRange
        {
            get { return _EachActionFrameRange; }
            set { _EachActionFrameRange = value; }
        }

        int _DirectionFrameNum;
        /// <summary>
        /// 英雄每方向总列数
        /// </summary>
        public int DirectionFrameNum
        {
            get { return _DirectionFrameNum; }
            set { _DirectionFrameNum = value; }
        }

        int _CurrentStartFrame = 0;
        /// <summary>
        /// 英雄当前动作开始图片列号
        /// </summary>
        public int CurrentStartFrame
        {
            get { return _CurrentStartFrame; }
            set { _CurrentStartFrame = value; }
        }

        int _CurrentEndFrame = 4;
        /// <summary>
        /// 英雄当前动作结束图片列号
        /// </summary>
        public int CurrentEndFrame
        {
            get { return _CurrentEndFrame; }
            set { _CurrentEndFrame = value; }
        }

        int _TotalWidth;
        /// <summary>
        /// 每张英雄合成大图总宽
        /// </summary>
        public int TotalWidth
        {
            get { return _TotalWidth; }
            set { _TotalWidth = value; }
        }

        int _TotalHeight;
        /// <summary>
        /// 每张英雄合成大图总高
        /// </summary>
        public int TotalHeight
        {
            get { return _TotalHeight; }
            set { _TotalHeight = value; }
        }

        int _SingleWidth = 150;
        /// <summary>
        /// 英雄单张图片宽,默认150
        /// </summary>
        public int SingleWidth
        {
            get { return _SingleWidth; }
            set { _SingleWidth = value; }
        }

        int _SingleHeight = 150;
        /// <summary>
        /// 英雄单张图片高,默认150
        /// </summary>
        public int SingleHeight
        {
            get { return _SingleHeight; }
            set { _SingleHeight = value; }
        }

        Point _Destination;
        /// <summary>
        /// 英雄移动目的地
        /// </summary>
        public Point Destination
        {
            get { return _Destination; }
            set { _Destination = value; }
        }

        int[] _Equipment = new int[9];
        /// <summary>
        /// 英雄装备代码(目前只有前2者)
        /// [0],衣服
        /// [1],武器
        /// [2],头盔
        /// [3],腰带
        /// [4],护手
        /// [5],鞋子
        /// [6],项链
        /// [7],戒指1
        /// [8],戒指2
        /// </summary>
        public int[] Equipment
        {
            get { return _Equipment; }
            set { _Equipment = value; }
        }

        //----------------------------------------------第20节增加--------------------------
        int _CenterX;
        /// <summary>
        /// 英雄单位图片左上角点与英雄图片中角色角底的X距离
        /// </summary>
        public int CenterX
        {
            get { return _CenterX; }
            set { _CenterX = value; }
        }

        int _CenterY;
        /// <summary>
        /// 英雄单位图片左上角点与英雄图片中角色角底的Y距离
        /// </summary>
        public int CenterY
        {
            get { return _CenterY; }
            set { _CenterY = value; }
        }

        bool _IsAStarMoving = false;
        /// <summary>
        /// 标志英雄是否正在A*寻路移动中
        /// </summary>
        public bool IsAStarMoving
        {
            get { return _IsAStarMoving; }
            set { _IsAStarMoving = value; }
        }

        int _FrameCounter = 1;
        /// <summary>
        /// 帧推进器
        /// </summary>
        public int FrameCounter
        {
            get { return _FrameCounter; }
            set { _FrameCounter = value; }
        }


        public Hero()
        {
            InitializeComponent();
            InitThread();
        }

        /// <summary>
        /// 初始化英雄生命线程
        /// </summary>
        private void InitThread()
        {
            _heroTimer.Tick += new EventHandler(_heroTimer_Tick);
            _heroTimer.Start();
        }

        int count = 1;
        void _heroTimer_Tick(object sender, EventArgs e)
        {
            Body.Source = cutImage(Environment.CurrentDirectory + @"\Players\J\130\run\left.png", count * 150, 0, 150, 150);
            count = count == 11 ? 0 : count + 1;

            //假如英雄动作发生改变，则调用ChangeAction()方法进行相关参数设置
            //if (OldAction != Action)
            //{
            //    ChangeAction();
            //}
            ////动态更改英雄图片源以形成英雄连续动作
            //Body.Source = HeroSource[(int)Direction, FrameCounter];
            //FrameCounter = FrameCounter == CurrentEndFrame ? CurrentStartFrame : FrameCounter + 1;
        }


        /// <summary>
        /// 改变英雄动作状态后激发的属性及线程改变
        /// </summary>
        private void ChangeAction()
        {
            switch (Action)
            {
                case Actions.Stop:
                    HeroTimer.Interval = TimeSpan.FromMilliseconds(150); //动作图片切换间隔
                    CurrentStartFrame = 0; //该动作在合成大图中的开始列
                    CurrentEndFrame = EachActionFrameRange[0] - 1; //该动作在合成大图中的结束列
                    OldAction = Actions.Stop; //将当前动作记录进OldAction里
                    break;
                case Actions.Run:
                    HeroTimer.Interval = TimeSpan.FromMilliseconds(100);
                    CurrentStartFrame = EachActionFrameRange[0];
                    CurrentEndFrame = EachActionFrameRange[0] + EachActionFrameRange[1] - 1;
                    OldAction = Actions.Run;
                    break;
                case Actions.Attack:
                    HeroTimer.Interval = TimeSpan.FromMilliseconds(120);
                    CurrentStartFrame = EachActionFrameRange[0] + EachActionFrameRange[1];
                    CurrentEndFrame = EachActionFrameRange[0] + EachActionFrameRange[1] + EachActionFrameRange[2] - 1;
                    OldAction = Actions.Attack;
                    break;
                case Actions.Magic:
                    HeroTimer.Interval = TimeSpan.FromMilliseconds(100);
                    CurrentStartFrame = EachActionFrameRange[0] + EachActionFrameRange[1] + EachActionFrameRange[2];
                    CurrentEndFrame = EachActionFrameRange[0] + EachActionFrameRange[1] + EachActionFrameRange[2] + EachActionFrameRange[3] - 1;
                    OldAction = Actions.Magic;
                    break;
                case Actions.Death:
                    HeroTimer.Interval = TimeSpan.FromMilliseconds(150);
                    CurrentStartFrame = EachActionFrameRange[0] + EachActionFrameRange[1] + EachActionFrameRange[2] + EachActionFrameRange[3];
                    CurrentEndFrame = EachActionFrameRange[0] + EachActionFrameRange[1] + EachActionFrameRange[2] + EachActionFrameRange[3] + EachActionFrameRange[4] - 1;
                    OldAction = Actions.Death;
                    break;
            }
            FrameCounter = CurrentStartFrame;
        }


        /// <summary>
        /// 从含有多个帧的合成图中截取某一帧
        /// </summary>
        /// <param name="imageUri">源图片Uri地址</param>
        /// <param name="x">该帧左上角在图片上的X坐标</param>
        /// <param name="y">该帧顶边在图片上的Y坐标</param>
        /// <param name="w">该帧宽度</param>
        /// <param name="h">该帧高度</param>
        /// <returns></returns>
        private BitmapSource cutImage(string imageUri, int x, int y, int w, int h)
        {
            return new CroppedBitmap(
                BitmapFrame.Create(new Uri(imageUri, UriKind.Relative)),
                new Int32Rect(x, y, w, h));
        }

    }
}
